package com.kly.order.controller;

import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.kly.dto.MemberBenefitsDto;
import com.kly.dto.ProductDto;
import com.kly.dto.ReduceBenefitsDto;
import com.kly.service.MemberBenefitsService;
import com.kly.service.MemberService;
import com.kly.service.TOrderService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("api/member")
public class MemberBenefitsController {

    @Resource
    MemberBenefitsService memberBenefitsService;

    @Resource
    MemberService memberService;

    @Resource
    TOrderService tOrderService;

    @Resource
    RedisClient redisClient;

    private final static String PRODUCT = "Product";

    @ResponseBody
    @RequestMapping(value = "/refCache")
    public void purchaseMember(ProductDto productDao) {
        String productJson = redisClient.get("key_{}_{}_{}",
                productDao.getAppCode(), PRODUCT, productDao.getProductCode());
    }

    @RequestMapping("/query")
    public Response getMemberInfo(String userCode) {
        return memberService.getMemberInfo(userCode, "sparksy");
    }

    @RequestMapping("/queryBenefits")
    public Response queryMemberBenefits(@Valid @RequestBody ReduceBenefitsDto reduceBenefits, BindingResult results) {
        Response<List<MemberBenefitsDto>> memberBenefits = memberBenefitsService.getMemberBenefits(
                reduceBenefits.getMemberCode(), reduceBenefits.getBenefitCode());
        if (!memberBenefits.isSuccess()) {
            return memberBenefits;
        } else {
            List<MemberBenefitsDto> memberBenefitsData = memberBenefits.getData();
            for (MemberBenefitsDto memberBenefitsDto : memberBenefitsData) {
                memberBenefitsDto.setStatus(1);
            }
            return Response.ok(memberBenefitsData);
        }
    }
}
